/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.presentationappearance;

import java.util.ArrayList;
import java.util.List;

import org.openaecfm.model.presentationappearance.IndexedTextureMap;
import org.openaecfm.model.presentationappearance.IndexedTriangleTextureMap;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.IntegerParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class IndexedTriangleTextureMapIO extends
		AbstractEntityInstanceInputImpl<IndexedTriangleTextureMap> implements
		IEntityInstanceOutput<IndexedTriangleTextureMap> {

	private IEntityInstanceUtilOutput<IndexedTextureMap> utilOutput;
	private IEntityInstanceUtilInput<IndexedTextureMap> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<IndexedTextureMap> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<IndexedTextureMap> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(IndexedTriangleTextureMap pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCINDEXEDTRIANGLETEXTUREMAP", appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set TexCoordIndex
		if (pojo.getTexCoordIndex() == null
				|| pojo.getTexCoordIndex().isEmpty()) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter texCoordIndex = new ListParameter();
			for (List<Integer> li : pojo.getTexCoordIndex()) {
				ListParameter l = new ListParameter();
				for (Integer i : li) {
					l.add(new IntegerParameter(i));
				}
				texCoordIndex.add(l);
			}
			ei.addParameter(texCoordIndex);
		}

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			IndexedTriangleTextureMap pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	@Override
	protected IndexedTriangleTextureMap doCreateObject() {
		return new IndexedTriangleTextureMap();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			IndexedTriangleTextureMap pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TexCoordIndex
		Parameter texCoordIndex = sei.getParameter(3);
		if (texCoordIndex.getNodeType() == NodeType.LIST) {
			ListParameter li = (ListParameter) texCoordIndex;
			if (pojo.getTexCoordIndex() == null) {
				pojo.setTexCoordIndex(new ArrayList<List<Integer>>());
			}
			for (Parameter p : li) {
				ListParameter l = (ListParameter) p;
				List<Integer> listi = new ArrayList<Integer>();
				for (Parameter pa : l) {
					IntegerParameter i = (IntegerParameter) pa;
					listi.add(i.toInteger());
				}
				pojo.getTexCoordIndex().add(listi);
			}
		}
	}
}
